package com.sky.controller.admin;/**
 * @author with葡萄成熟时
 * @version 1.0
 */

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @description:  删除缓存
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午3:47 2024/11/12
     */
    private void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
        log.info("缓存清理执行");
    }
    /**
     * @description: 新增菜品
     * @param: [dishDTO]
     * @return: com.sky.result.Result
     * @author Wait葡萄成熟时
     * @date: 下午10:26 2024/11/5
     */
    @ApiOperation("新增菜品")
    @PostMapping("/admin/dish")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品");


        dishService.addDish(dishDTO);
        //清理缓存
        Long categoryId = dishDTO.getCategoryId();
        String key="dish_"+categoryId;
        cleanCache(key);

        return Result.success();


    }

    /**
     * @description: 分页查询
     * @param:[dishPageQueryDTO]
     * @return: com.sky.result.Result<com.sky.result.PageResult>
     * @author Wait葡萄成熟时
     * @date: 上午10:10 2024/11/7
     */
    @ApiOperation("分页查询")
    @GetMapping("/admin/dish/page")

    public Result<PageResult> selectDishPage(DishPageQueryDTO dishPageQueryDTO) {
        //要求返回的是PageResult
        PageResult pageResult = dishService.selectDishPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @description: 删除菜品
     * @param: [ids]
     * @return: com.sky.result.Result
     * @author Wait葡萄成熟时
     * @date: 下午5:38 2024/11/7
     */
    @ApiOperation("删除菜品")
    @DeleteMapping("/admin/dish")
    public Result deleteDish(@RequestParam List<Long> ids) {

        dishService.deleteDish(ids);

        //删除所有缓存
        cleanCache("dish_*");
        return Result.success();

    }
    /**
     * @description: 根据id查询菜品
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.vo.DishVO>
     * @author Wait葡萄成熟时
     * @date: 下午9:17 2024/11/7
     */
    @GetMapping("/admin/dish/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> selectDishById(@PathVariable Long id) {

        DishVO dishVO = dishService.selectDishById(id);

        return Result.success(dishVO);
    }

    /**
     * @description: 修改菜品信息
     * @param: [dishDTO]
     * @return: com.sky.result.Result
     * @author Wait葡萄成熟时
     * @date: 下午10:22 2024/11/7
     */
    @PutMapping("/admin/dish")
    @ApiOperation("修改菜品信息")
    public Result updateDish(@RequestBody DishDTO dishDTO) {

        dishService.updateDish(dishDTO);

        //删除所有缓存
        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/admin/dish/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> selectByCategoryId(@RequestParam Long categoryId){

       List <Dish> list=dishService.selectByCategoryId(categoryId);
        return Result.success(list);


    }
    /**
     * @description: 菜品起售、停售
     * @param: [status, id]
     * @return: com.sky.result.Result
     * @author Wait葡萄成熟时
     * @date: 上午12:55 2024/11/8
     */
    @PostMapping("/admin/dish/status/{status}")
    @ApiOperation("菜品起售、停售")

    public Result sellOrNot(@PathVariable Integer status,@RequestParam Long id){
        dishService.sellOrNot(status,id);

        //删除所有缓存
        cleanCache("dish_*");
    return Result.success();

    }
}
